1
|
|
|
/** |
2
|
|
|
* A module for Memory game brain. |
3
|
|
|
* |
4
|
|
|
* @module |
5
|
|
|
*/ |
6
|
|
|
"use strict"; |
7
|
|
|
|
8
|
|
|
class Gamebrain { |
9
|
|
|
/** |
10
|
|
|
* @constructor |
11
|
|
|
*/ |
12
|
|
|
constructor() { |
13
|
12 |
|
this.colors = [ |
14
|
|
|
"blackplayer", |
15
|
|
|
"redplayer", |
16
|
|
|
"blueplayer", |
17
|
|
|
"greenplayer", |
18
|
|
|
"pinkplayer", |
19
|
|
|
"orangeplayer", |
20
|
|
|
"yellowplayer", |
21
|
|
|
"limegreenplayer", |
22
|
|
|
"purpleplayer" |
23
|
|
|
]; |
24
|
12 |
|
this.usedcolors = []; |
25
|
12 |
|
this.players = new Map(); |
26
|
12 |
|
this.playersscore = new Map(); // filles with players and cardvalues of pairs |
27
|
12 |
|
this.paircheck = ""; // temp holder for checking if pair fliped. |
28
|
12 |
|
this.playerinturnmarker = 0; // for moving forward to next player in turn. |
29
|
12 |
|
this.playerinturn; // nickname of player in turn. |
|
|
|
|
30
|
12 |
|
this.numberofplayermoves = 0; // number of moves player in turn has done. |
31
|
12 |
|
this.cardvalues = []; // for storing flipped cards values. |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adding player to Map (nickname, colorclass) |
36
|
|
|
* |
37
|
|
|
* @param {string} player - uniqified nickname |
38
|
|
|
* @param {string} color - color class for that player |
39
|
|
|
*/ |
40
|
|
|
addPlayer(player, color) { |
41
|
20 |
|
this.players.set(player, color); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
makeMove() { |
45
|
|
|
this.numberofplayermoves += 1; |
46
|
|
|
// console.log("this.numberofplayermoves: " + this.numberofplayermoves); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
setCardValue(player, colorclass, cardvalue) { |
50
|
|
|
// finns det inget kort här så lägga bara till |
51
|
|
|
// finns det redan ett kort kolla om det har samma värde. |
52
|
|
|
// har det samma värde är det ett par och numberofplayermoves nollställs |
53
|
|
|
// man tar även bort alla kort i cardvalues |
54
|
|
|
// är det inte ett par nollställs inte inte numberofplayers men cardvalues töms. |
55
|
4 |
|
var cardvalues = this.cardvalues; |
56
|
|
|
|
57
|
4 |
|
if (cardvalues.length === 0) { |
58
|
2 |
|
this.cardvalues.push(cardvalue); |
59
|
|
|
} else { |
60
|
|
|
// det finns kort i cardvalues |
61
|
4 |
|
if (cardvalues.indexOf(cardvalue) != -1) { |
62
|
|
|
// det är samma värde som nyinkomna. |
63
|
2 |
|
this.numberofplayermoves = 0; |
64
|
|
|
// lägger till spelares colorclass med kort i poänglistan. |
65
|
2 |
|
this.playersscore.set(player, cardvalue); |
66
|
2 |
|
this.paircheck = colorclass; |
67
|
|
|
} |
68
|
2 |
|
this.cardvalues = []; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set and get player in turn. |
74
|
|
|
* |
75
|
|
|
* @param {boolean} firstround - set to false by default. |
76
|
|
|
* |
77
|
|
|
* @return {string} nickname of player who´s turn it is. |
78
|
|
|
*/ |
79
|
|
|
setActivePlayer(firstround = false) { |
80
|
6 |
|
var players = this.getPlayersNicks(); |
81
|
6 |
|
var numberofplayermoves = this.numberofplayermoves; |
82
|
|
|
|
83
|
|
|
// console.log("numberofplayermoves: " + numberofplayermoves); |
84
|
6 |
|
if (firstround) { |
85
|
2 |
|
this.playerinturnmarker = Math.floor(Math.random() * players.length); |
86
|
2 |
|
this.playerinturn = players[this.playerinturnmarker]; |
87
|
2 |
|
this.numberofplayermoves = 0; |
88
|
|
|
// console.log("firstround - this.playerinturnmarker: " + this.playerinturnmarker); |
89
|
|
|
// console.log("firstround - this playerinturn: " + this.playerinturn); |
90
|
|
|
} else { |
91
|
4 |
|
if (numberofplayermoves == 2) { |
92
|
|
|
// console.log("Selecting new active player."); |
93
|
2 |
|
var playerinturnmarker = this.playerinturnmarker; |
94
|
|
|
|
95
|
4 |
|
if (playerinturnmarker < players.length-1) { |
96
|
2 |
|
this.playerinturnmarker += 1; |
97
|
|
|
} else { |
98
|
|
|
this.playerinturnmarker = 0; |
99
|
|
|
} |
100
|
|
|
// console.log("this.playerinturnmarker: " + this.playerinturnmarker); |
101
|
2 |
|
this.playerinturn = players[this.playerinturnmarker]; |
102
|
|
|
// console.log("New player in turn: " + this.playerinturn); |
103
|
2 |
|
this.numberofplayermoves = 0; |
104
|
|
|
} |
105
|
|
|
} |
106
|
6 |
|
return this.playerinturn; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
dropPlayer(player) { |
110
|
|
|
this.players.delete(player); |
111
|
|
|
// console.log("Players (accroding to GameBrain)"); |
112
|
|
|
// console.log(this.players); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Decide unique color for 'player' and add player to player Map |
117
|
|
|
* |
118
|
|
|
* @param {string} player - nickname of player to add to game |
119
|
|
|
* |
120
|
|
|
* @return {string} colorclass for player |
121
|
|
|
*/ |
122
|
|
|
setPlayerColor(player) { |
123
|
6 |
|
var color = "blackplayer"; |
124
|
6 |
|
var notuniquecolor = true; |
125
|
|
|
|
126
|
6 |
|
while (notuniquecolor) { |
127
|
6 |
|
color = this.colors[Math.floor(Math.random() * this.colors.length)]; |
128
|
6 |
|
if (this.usedcolors.indexOf(color) === -1) { |
129
|
6 |
|
notuniquecolor = false; |
130
|
6 |
|
this.usedcolors.push(color); |
131
|
6 |
|
this.addPlayer(player, color); |
132
|
|
|
} |
133
|
|
|
} |
134
|
6 |
|
return color; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Takes a nickname and add a figure to make nickname unique. |
139
|
|
|
* |
140
|
|
|
* @param {string} incomingnickname, from new user. |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @return {string} unique nickname. |
143
|
|
|
*/ |
144
|
|
|
uniquifyname(incomingnickname) { |
145
|
6 |
|
var incomingnicknamelength = incomingnickname.length; |
146
|
|
|
var stringdiff; |
147
|
6 |
|
var users = this.getPlayersNicks(); |
148
|
6 |
|
var counter = 2; |
149
|
|
|
|
150
|
|
|
// while(users.indexOf(incomingnickname) !== -1) { |
151
|
6 |
|
while (users.indexOf(incomingnickname) !== -1) { |
152
|
6 |
|
if (incomingnickname.length == incomingnicknamelength) { |
153
|
4 |
|
incomingnickname = incomingnickname + counter; |
154
|
|
|
} else { |
155
|
2 |
|
stringdiff = incomingnickname.length - incomingnicknamelength; |
156
|
2 |
|
incomingnickname = incomingnickname.slice(0, -stringdiff); |
157
|
2 |
|
incomingnickname = incomingnickname + counter; |
158
|
|
|
} |
159
|
6 |
|
counter++; |
160
|
|
|
} |
161
|
6 |
|
return incomingnickname; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
getPlayersNicks() { |
165
|
12 |
|
return [...this.players.keys()]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
getPlayersColors() { |
169
|
|
|
return [...this.players.values()]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Used to check if pair was flipped |
174
|
|
|
* |
175
|
|
|
* @return {string} result - 'nopair' if no pair was flipped. Otherwise players colorclass. |
176
|
|
|
*/ |
177
|
|
|
gotPair() { |
178
|
|
|
var result = ""; |
179
|
|
|
|
180
|
4 |
|
if (this.paircheck != "") { |
181
|
|
|
result = this.paircheck; |
182
|
|
|
this.paircheck = ""; |
183
|
|
|
} |
184
|
|
|
return result; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Export module |
189
|
|
|
module.exports = Gamebrain; |
190
|
|
|
|